package com.zack.gank.gankandroid;

import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;

import com.gank.zack.ganklibrary.network.retrofit.RetrofitHelper;
import com.zack.gank.gankandroid.api.GankInterface;
import com.zack.gank.gankandroid.gankandroid.R;
import com.zack.gank.gankandroid.main.main.MainAdapter;
import com.zack.gank.gankandroid.main.main.FindFragment;
import com.zack.gank.gankandroid.main.main.ReleaseFragment;
import com.zack.gank.gankandroid.main.main.SexyFragment;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MainActivity extends AppCompatActivity {
    ViewPager  viewpager;
    MenuItem prevMenuItem;
    BottomNavigationView bottomNavigationView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


       /*GankInterface gankInterface= RetrofitHelper.createApi(GankInterface.class);
         Call<TestHome> objectCall=gankInterface.getGankHomeData("2017/07/11");


        objectCall.enqueue(new Callback<TestHome>() {
            @Override
            public void onResponse(Call<TestHome> call, Response<TestHome> response) {
                com.orhanobut.logger.Logger.v(response.body().getResults().get(0).getContent());
            }

            @Override
            public void onFailure(Call<TestHome> call, Throwable t) {

            }
        });*/

        inintView();



    }

    private void inintView(){
        bottomNavigationView = (BottomNavigationView) findViewById(R.id.btnv_buttom);
        viewpager  = (ViewPager) findViewById(R.id.viewpager);


        bottomNavigationView.setOnNavigationItemSelectedListener(
                new BottomNavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                        switch (item.getItemId()) {
                            case R.id.home:
                                viewpager.setCurrentItem(0);
                                break;
                            case R.id.favorite:
                                viewpager.setCurrentItem(1);
                                break;
                            case R.id.me:
                                viewpager.setCurrentItem(2);
                                break;
                        }
                        return false;
                    }
                });


        viewpager.setOffscreenPageLimit(3);

        viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                //
                if (prevMenuItem != null) {
                    prevMenuItem.setChecked(false);
                } else {
                    bottomNavigationView.getMenu().getItem(0).setChecked(false);
                }
                bottomNavigationView.getMenu().getItem(position).setChecked(true);
                prevMenuItem = bottomNavigationView.getMenu().getItem(position);

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


        MainAdapter mainAdapter=new MainAdapter(getSupportFragmentManager());
        mainAdapter.addFragment(new FindFragment());
        mainAdapter.addFragment(new SexyFragment());
        mainAdapter.addFragment(new ReleaseFragment());
        viewpager.setAdapter(mainAdapter);
    }

}
